package android.slc.appbase.ui.vd;

import android.os.Bundle;
import android.slc.appbase.entity.ErrorMsg;
import android.slc.appbase.utils.AppIntentUtils;
import android.slc.core.vm.SingleLiveEvent;

import androidx.databinding.Observable;
import androidx.databinding.ObservableField;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseListCommonVmBox<T extends Serializable> extends BaseDataChangeVmBox implements BaseRegisterActivityFunc.IIntegrationRegisterActivityFunc {
    protected ObservableField<List<T>> dataListOf = new ObservableField<>(new ArrayList<>());
    protected ObservableField<ErrorMsg> errorMsgOf = new ObservableField<>();

    public SingleLiveEvent<Bundle> showItemClickUiOf = new SingleLiveEvent<>();

    private BaseRegisterActivityFunc.IntentTargetWrapper intentTargetWrapper;
    public final BaseRegisterActivityFunc<Serializable> actionRegisterActivityFunc;

    public BaseListCommonVmBox() {
        this.actionRegisterActivityFunc = createActionRegisterActivityFunc();
        if (this.actionRegisterActivityFunc != null && this.actionRegisterActivityFunc.getOnTargetActivityResultCallback() == null) {
            this.actionRegisterActivityFunc.setOnActivityResultCallback(new SimpleRegisterActivityFunc.OnActivityResultCallback<Serializable>() {
                @Override
                public void onIntentData(Serializable data) {
                    BaseListCommonVmBox.this.notifyRefreshByGlobal(data);
                }

                @Override
                public void onIntentBundle(Bundle data) {
                    BaseListCommonVmBox.this.onActivityResult(data);
                }
            });
        }
    }

    @Override
    public BaseRegisterActivityFunc<Serializable> createActionRegisterActivityFunc() {
        return new SimpleRegisterActivityFunc<>(getGotoTargetActivityItw());
    }

    /**
     * 获取item点击后要取得activityClass
     *
     * @return
     */
    @Override
    public final BaseRegisterActivityFunc.IntentTargetWrapper getGotoTargetActivityItw() {
        if (intentTargetWrapper == null) {
            intentTargetWrapper = createGotoTargetActivityItw();
        }
        return intentTargetWrapper;
    }

    protected BaseRegisterActivityFunc.IntentTargetWrapper createGotoTargetActivityItw() {
        return null;
    }

    public Observable getDataListOf() {
        return dataListOf;
    }

    public List<T> getDataList() {
        return dataListOf.get();
    }

    public Observable getErrorMsgObservable() {
        return errorMsgOf;
    }

    public ErrorMsg getErrorMsg() {
        return errorMsgOf.get();
    }

    /**
     * 刷新
     */
    public abstract void refresh();

    /**
     * 点击item
     *
     * @param index
     * @param data
     */
    public void itemClick(int index, T data) {
        if (getGotoTargetActivityItw() != null) {
            this.showItemClickUiOf.setValue(AppIntentUtils.getBundleByData(data));
        }
    }

    /**
     * 长按item
     *
     * @param index
     * @param data
     */
    public void itemLongClick(int index, T data) {
    }


    /**
     * 相应失败
     *
     * @param errorCode
     * @param errorMessage
     */
    protected void onFailed(int errorCode, String errorMessage) {
        errorMsgOf.set(new ErrorMsg(errorCode, errorMessage));
    }

}
